package com.artPreter.service.impl;

import com.artPreter.pojo.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.artPreter.mapper.ArticleMapper;
import com.artPreter.service.IArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 文章内容表 服务实现类
 * </p>
 *
 * @author jarvis
 * @since 2024-11-21
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {


    @Override
    public List<Article> getRecommendArticles(int i) {
        List<Article> result = new ArrayList<>();
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getIsRecommend, 1);
        queryWrapper.eq(Article::getStatus,1);
        queryWrapper.last("limit " + i);
        if(i>0){
            result = this.baseMapper.selectList(queryWrapper);
        }
        return result;
    }

    @Override
    public List<Article> getIndexArticles(int i) {
        List<Article> result = new ArrayList<>();
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getIsIndex, 1);
        queryWrapper.last("limit " + i);
        queryWrapper.eq(Article::getStatus,1);
        if(i>0){
            result = this.baseMapper.selectList(queryWrapper);
        }
        return result;
    }

    @Override
    public List<Article> getArticleList(String articleId) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getArticleId,articleId);
        queryWrapper.eq(Article::getStatus,1);
        List<Article> result = this.baseMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public List<Article> getDepartRelateArticleList(DepartContentSection departContentSection, int i) {
        String contentId = departContentSection.getContentId();
        QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();
        articleQueryWrapper.eq("s.content_id", contentId);
        articleQueryWrapper.last("limit " + i);
        List<Article> result = this.baseMapper.getDepartRelateArticleList(articleQueryWrapper);
        return result;
    }

    @Override
    public List<Article> getArtistRelateArticleList(ArtistContentSection artistContentSection, int i) {
        String contentId = artistContentSection.getContentId();
        QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();
        articleQueryWrapper.eq("s.content_id", contentId);
        articleQueryWrapper.eq("b.status", 1);
        if(i==-99){

        }else{
            articleQueryWrapper.last("limit " + i);
        }
        articleQueryWrapper.orderByDesc("s.sort");
        List<Article> result = this.baseMapper.getDepartRelateArticleList(articleQueryWrapper);
        return result;
    }

    @Override
    public List<Article> getArticleByName(Article article) {
        LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Article> result = new ArrayList<>();
        if(article.getName()!=null){
            lambdaQueryWrapper.like(Article::getName,article.getName());
        }
        result = this.baseMapper.selectList(lambdaQueryWrapper);
        return result;
    }

    @Override
    public IPage<Article> getPageRelativeFull(Integer currentPage, Integer size, String contentId) {

        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus,1);
        queryWrapper.orderByDesc(Article::getId);
        System.out.println(contentId);
        queryWrapper.exists("SELECT 1 FROM jv_content_article_relative WHERE jv_content_article_relative.content_id = '"+contentId+"' and jv_content_article_relative.article_id = jv_article.article_id");
        IPage<Article> articleIPage = new Page<>(currentPage,size);
        articleIPage = this.baseMapper.selectPage(articleIPage, queryWrapper);
        return articleIPage;
    }

    @Override
    public IPage<Article> getDepartRelatedArticlePageList(DepartContentSection departContentSection, Integer currentPage) {
        IPage<Article> articleIPage = new Page<>(currentPage,10);
        String contentId = departContentSection.getContentId();
        QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();
        articleQueryWrapper.eq("s.content_id", contentId);
        articleQueryWrapper.eq("b.status",1);
        articleQueryWrapper.orderByDesc("s.sort");
        articleIPage = this.baseMapper.getDepartRelateArticlePageList(articleIPage,articleQueryWrapper);
        return articleIPage;
    }

    @Override
    public Article getArticleByNumberId(Integer aid) {
        Article article = this.baseMapper.selectById(aid);
        return article;
    }

    @Override
    public IPage<Article> getEventExhibitionRelatedArticlePageList(EventExhibitionContentSection eventExhibitionContentSection, Integer currentPage) {
        IPage<Article> articleIPage = new Page<>(currentPage,10);
        String contentId = eventExhibitionContentSection.getContentId();
        QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();
        articleQueryWrapper.eq("s.content_id", contentId);
        articleQueryWrapper.eq("b.status",1);
        articleQueryWrapper.orderByDesc("b.create_date");
        articleIPage = this.baseMapper.getDepartRelateArticlePageList(articleIPage,articleQueryWrapper);
        return articleIPage;
    }

    @Override
    public List<Article> getArticleListById(Integer id) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getId,id);
        queryWrapper.eq(Article::getStatus,1);
        List<Article> result = this.baseMapper.selectList(queryWrapper);
        return result;
    }


}
